

import java.awt.Polygon;
import java.net.URLConnection;
import java.net.URL;
import java.net.Socket;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Locale;

/**
 *
 * @author Claus Wind
 */
public class SvgReader {

    private Scanner scanner;
    private String charsetName = "ISO-8859-1";
    private Locale usLocale = new Locale("en", "US");
    private boolean sysO = false;

    public SvgReader(String s) {
        try {
            // first try to read file from local file system
            File file = new File(s);
            if (file.exists()) {
                scanner = new Scanner(file, charsetName);
                scanner.useLocale(usLocale);
                return;
            }

            // next try for files included in jar
            URL url = getClass().getResource(s);

            // or URL from web
            if (url == null) { url = new URL(s); }

            URLConnection site = url.openConnection();
            InputStream is     = site.getInputStream();
            scanner            = new Scanner(new BufferedInputStream(is), charsetName);
            scanner.useLocale(usLocale);
        }
        catch (IOException ioe) {
            System.err.println("Could not open " + s);
        }
    }

    public ArrayList<Polygon> makeOutline() {
        return makeOutline(false);
    }
    public ArrayList<Polygon> makeOutline(boolean SysO) {
        this.sysO = SysO;
        ArrayList<String> paths = new ArrayList();
        String line = "";
        while(hasNextLine()) {
            line = readLine();
            if(line.contains("#F2EFE9")) {
                paths.add(line);
            }
        }
        if(SysO)System.out.println("Step 1!!");
        ArrayList<Polygon> poly = new ArrayList();
        for(String s : paths) {
            double[] CIL = coordinatesInLine(s);
            int[] xPoints = new int[CIL.length/2];
            int[] yPoints = new int[CIL.length/2];
            for(int i = 0; i < CIL.length/2; i++) { //FIXME
                xPoints[i] = (int)CIL[i];
                yPoints[i] = (int)CIL[i+1];
                if(SysO)System.out.println(xPoints[i] + "," + yPoints[i]);
            }
            Polygon p = new Polygon(xPoints,yPoints,CIL.length/2);
            poly.add(p);
        }
        if(SysO)System.out.println("Step 2!!");
        return poly;
    }


    private String readLine() {
        String line = null;
        try                 { line = scanner.nextLine(); }
        catch (Exception e) {                            }
        if(line.charAt(line.length()-1) != '>') {
            addLine(line);
        }
        return line;
    }

    private String addLine(String s) {
        String line = s;
        line += readLine();
        return line;
    }

    public boolean hasNextLine() {
        return scanner.hasNextLine();
    }

    private double[] coordinatesInLine(String s) {
        if(sysO)System.out.println(s);
        boolean write = false;
        int p = 0;
        int t = 0;
        String[] value = new String[50];
        char[] mods = new char[50];
        String[] temp = new String[5];
        if(sysO)System.out.println("Length of input string: " + s.length());
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == 'M') { //Move pointer to.
                write = true;
                i++;
            }
            if(write) {
                if(s.charAt(i) == '.') { //Decimal, in format like "654.345.234" there are two points: "654.345" and "654.234".
                    if(t==0){
                        temp[0] = value[p];
                        if(sysO)System.out.println("Char before . is: " + value[p] + " " + temp[0]);
                        t = 1;
                    }
                    if(t==1){
                        value[p] = temp[0];
                        p++;
                        value[p] = temp[0];
                        t = 2;
                    }
                    else {
                        if(sysO)System.err.println("Something went wrong with the syntax.. Too many '.' in a row");
                    }
                }
                else if(s.charAt(i) == ',') { //Point
                    //if(SysO)System.out.println("Seperator found: '" + s.charAt(i)+ "'(" + p + ")");
                    if(s.charAt(i-1) != '-') p++;

                    if(p != 0 && mods[p-1]!='-') mods[p] = ',';
                    t = 0;
                }
                else if(s.charAt(i) == '-') { //Negative Value
                    //if(SysO)System.out.println("- modifier reached: '" + s.charAt(i)+ "'(" + p + ")");
                    p++;
                    mods[p] = '-';
                    t = 0;
                }
                else if(s.charAt(i) == 'L' || s.charAt(i) == 'l') { //LineTo
                    //if(SysO)System.out.println("L modifier reached: '" + s.charAt(i)+ "'(" + p + ")");
                    if(s.charAt(i-1) != '-') p++;
                    if(p != 0 && mods[p-1]!='-') mods[p] = 'L';
                    t = 0;
                }
                else if(s.charAt(i) == 'V' || s.charAt(i) == 'v') { //Vertical LineTo (same x-value)
                    //if(SysO)System.out.println("V modifier reached: '" + s.charAt(i)+ "'(" + p + ")");
                    if(s.charAt(i-1) != '-') p++;
                    if(value[p] == null) value[p] = "";
                    value[p] += value[p - 2];
                    p++;
                    t = 0;
                }
                else if(s.charAt(i) == 'H' || s.charAt(i) == 'h') { //Horizontal LineTo (same y-value)
                    //if(SysO)System.out.println("H modifier reached: '" + s.charAt(i)+ "'(" + p + ")");
                    if(mods[p] != '-') p++;
                    if(value[p] == null) value[p] = "";
                    value[p] += value[p - 1];
                    p++;
                    t = 0;
                }
                else if(s.charAt(i) == '"') { //End of data reached
                    //if(SysO)System.out.println("We are done here; found: '" + s.charAt(i)+ "'");
                    i = s.length();
                }
                else { //Simply add the numbers to a string so that they can be converted in the
                    //if(SysO)System.out.println("Int added to value[]: '" + s.charAt(i)+ "'(" + p + ")");
                    /*if(temp[0] != null) {

                        if(temp[1] != null) {
                            temp[0] = temp[3];
                            temp[0] = null;
                            value[p] = temp[3];
                            p++;
                        }
                    }*/
                    if(value[p] == null) value[p] = "";
                    if(t==0) value[p] += s.charAt(i);
                }
            }
        }
        double[] points = new double[p];
        for(int i = 0; i <= p; i++) {
            if(sysO)System.out.println("Printing: " + value[i] + ", p = " + p);
            if(value[i] == null) break;
            points[i] = Integer.parseInt(value[i]);
            if(mods[i]=='-') points[i] = -points[i];
        }
        return points;
    }

    public static void main(String[] args) {
        SvgReader svg = new SvgReader("..\\instance\\map2.svg");
        svg.makeOutline(true);
    }
}